﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;
using System.Text.RegularExpressions;

namespace ITOMMS.Web.InsideManage
{
    public partial class SmsSend : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //内部员工
                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery(" CompanyName='上海汪洋网络' and RoleGroup!=" + PubEnums.RoleGroup.技术主管.ToString("D")), this.ddlUserMobile, "Mobile", "RealName");//绑定员工手机号及姓名
                this.ddlUserMobile.Items.Add(new ListItem("手动输入手机号", "0"));
            }
        }

        protected void btnSend_ServerClick(object sender, EventArgs e)
        {
            string sReceiveMobile = "";
            string sSmsContent = "";

            if (this.ddlUserMobile.SelectedValue == "-1")
            {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('请输入接收手机号。',300,180,'提示',null);</script>");
                    return;
            }
            else if (this.txtMobile.Text.Trim() == "")
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('请输入接收手机号。',300,180,'提示',null);</script>");
                return;
            }

            if (this.txtSmsContent.Value.Trim() == "")
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('请输入短信内容。',300,180,'提示',null);</script>");
                return;
            }
            if (this.txtSmsContent.Value.Trim().Length > 70)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('短信内容过长，请控制在70个字符以内。',300,180,'提示',null);</script>");
                 return;
            }

            

            if (this.ddlUserMobile.SelectedValue != "0" && this.ddlUserMobile.SelectedValue != "")
                sReceiveMobile = this.ddlUserMobile.SelectedValue.Trim();
            else if (this.txtMobile.Text.Trim() != "")
                sReceiveMobile = this.txtMobile.Text.Trim();

            sSmsContent = this.txtSmsContent.Value.Trim();


            Regex reg = new Regex(@"(130|131|132|155|156|185|186|133|134|135|136|137|138|139|150|151|152|153|158|159|187|188|189)\d{8}");
            if (!reg.IsMatch(sReceiveMobile))
            {
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "请输入正确的手机号");
                return;
            }



            string sSenderMobile = System.Configuration.ConfigurationManager.AppSettings["SmsSender"];
            string sSmsPWD = System.Configuration.ConfigurationManager.AppSettings["SmsPWD"];
            tbMobileMessageEntity msg = new tbMobileMessageEntity();
            msg.SendMobile = sSenderMobile;
            msg.ReceiveMobile = sReceiveMobile;
            msg.MessageContent = sSmsContent;
            msg.SendDate = System.DateTime.Now;
            msg.SendState = true;

            SendSMS(sSenderMobile, sSmsPWD, sReceiveMobile, sSmsContent);
            msg.Save(msg);

            MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "发送成功");
        }


        private void SendSMS(string sendmobile, string pass, string receivermobile, string smscontent)
        {
            Uri u = new Uri("http://sms.api.bz/fetion.php?username=" + sendmobile + "&password=" + pass + "&sendto=" + receivermobile + "&message=" + smscontent + "");
            System.Net.HttpWebRequest myrequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(u);//创建http请求
            myrequest.Timeout = 5000;//设置超时时间
            myrequest.Headers.Set("Pragma", "no-cache");//设置头
            System.Net.HttpWebResponse myresponse = (System.Net.HttpWebResponse)myrequest.GetResponse();
            //Response.Write(myresponse.StatusDescription.ToString() + ":" + myresponse.StatusCode.ToString());
        }

        protected void ddlUserMobile_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtMobile.Text = this.ddlUserMobile.SelectedValue.Trim();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SmsSendList.aspx", true);
        }

    }
}
